#include "stack.h"

bool stack_create(lp_my_stack out_MyStack, int in_buffer_size)
{
	memset(out_MyStack,0,sizeof(my_stack));
	if(!(out_MyStack->stack_buffer = (int*)malloc(sizeof(int)*in_buffer_size)))
	{
		return false;
	}
	out_MyStack->stack_buffer_size = sizeof(int) * in_buffer_size;
	out_MyStack->stack_top = out_MyStack->stack_buffer;
	out_MyStack->stack_count = 0;
	out_MyStack->stack_count_max = in_buffer_size;
	return true;
}

bool stack_push(lp_my_stack MyStack, int in_param)
{
	if(!MyStack)
		return false;
	if(MyStack->stack_count_max - MyStack->stack_count <= 0)
		return false;
	*(MyStack->stack_top) = in_param;
	(MyStack->stack_top)++;
	(MyStack->stack_count)++;
	return true;
}

bool stack_pop(lp_my_stack MyStack, int* out_param)
{
	if(!MyStack)
		return false;
	if(MyStack->stack_count == 0)
		return false;
	(MyStack->stack_top)--;
	*out_param = *(MyStack->stack_top);
	(MyStack->stack_count)--;
	return true;
}

bool stack_get_top(lp_my_stack MyStack,int* out_param)
{
	if(!MyStack)
	  return false;
	if(MyStack->stack_count == 0)
	  return false;
	*out_param = *(MyStack->stack_top - 1);
	return true;
}

bool stack_get_count(lp_my_stack MyStack,int* out_param)
{
	if(!MyStack)
		return false;
	*out_param = MyStack->stack_count;
	return true;
}

bool stack_zero(lp_my_stack MyStack)
{
	memset(MyStack->stack_buffer,0,MyStack->stack_buffer_size);
	MyStack->stack_top = MyStack->stack_buffer;
	MyStack->stack_count = 0;
	return true;
}

bool stack_destroy(lp_my_stack MyStack)
{
	free(MyStack->stack_buffer);
	memset(MyStack,0,sizeof(my_stack));
	return true;
}
